package com.example.education.service.impl;

import com.example.education.exception.ParamException;
import com.example.education.dao.QuestionMapper;
import com.example.education.model.Question;
import com.example.education.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    QuestionMapper questionMapper;

    @Override
    public boolean insertQuestion(Question question) throws ParamException {
        if(question==null){
            throw new ParamException("参数为空");
        }else if (question.getContent()==null || "".equals(question.getContent())){
            throw new ParamException("Content为空");
        }else if (question.getCourseId()==null){
            throw new ParamException("CourseId为空");
        }else if (question.getUserId()==null){
            throw new ParamException("UserId为空");
        }
        question.setCreatTime(new Date());
        Integer result =  questionMapper.insertQuestion(question);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteQuestion(Integer questionId) throws ParamException {
        if(questionId==null){
            throw new ParamException("参数为空");
        }
        Integer result =  questionMapper.deleteQuestion(questionId);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean topQuestion(Integer questionId) throws ParamException {
        if(questionId==null){
            throw new ParamException("参数为空");
        }
        Integer result =  questionMapper.topQuestion(questionId);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean unTopQuestion(Integer questionId) throws ParamException {
        if(questionId==null){
            throw new ParamException("参数为空");
        }
        Integer result =  questionMapper.unTopQuestion(questionId);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Question getQuestionById(Integer questionId) throws ParamException {
        if(questionId==null){
            throw new ParamException("参数为空");
        }
        return questionMapper.getQuestionById(questionId);
    }

    @Override
    public List<Question> getQuestionByCourseId(Integer courseId) throws ParamException {
        if(courseId==null){
            throw new ParamException("参数为空");
        }
        return questionMapper.getQuestionByCourseId(courseId);
    }
}
